home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / hAWK project / AWK Source / REGEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-07  |  47.1 KB  |  1,797 lines  |  [TEXT/TOPC]

  1. /* Extended regular expression matching and search. */
  2. /* Copyright © 1985, 1991 the Free Software Foundation, Inc.
  3.  *         This file is part of GAWK, the GNU implementation of the
  4.  * AWK Progamming Language, modified for the Macintosh (also called hAWK).
  5.  *         GAWK is free software; you can redistribute or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or any later version.
  8.  *         GAWK is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *         You should have received a copy of the GNU General Public License
  13.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  14.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  * Modified for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  16.  */
  17.  
  18. /*#include <malloc.h>*/
  19. #include "AWK.H"
  20.  
  21. /* Carriage Return of the moment... */
  22. #define CR '\n'
  23.  
  24. static  void init_syntax_once(void );
  25. extern  short re_set_syntax(short syntax);
  26. extern  char *re_compile_pattern(char *pattern,short size,struct re_pattern_buffer *bufp);
  27. static  short store_jump(char *from,char opcode,char *to);
  28. static  short insert_jump(char op,char *from,char *to,char *current_end);
  29. extern  void re_compile_fastmap(struct re_pattern_buffer *bufp);
  30. extern  short re_search(struct re_pattern_buffer *pbufp,char *string,short size,short startpos,short range,struct re_registers *regs);
  31. extern  short re_search_2(struct re_pattern_buffer *pbufp,char *string1,short size1,char *string2,short size2,short startpos,short range,struct re_registers *regs,short mstop);
  32. extern  short re_match(struct re_pattern_buffer *pbufp,char *string,short size,short pos,struct re_registers *regs);
  33. extern  short re_match_2(struct re_pattern_buffer *pbufp,unsigned char *string1,short size1,unsigned char *string2,short size2,short pos,struct re_registers *regs,short mstop);
  34. static  short bcmp_translate(unsigned char *s1,unsigned char *s2,short len,unsigned char *translate);
  35. extern  char *re_comp(char *s);
  36. extern  short re_exec(char *s);
  37.  
  38.  
  39. /* To test, compile with -Dtest.
  40.  This Dtestable feature turns this into a self-contained program
  41.  which reads a pattern, describes how it compiles,
  42.  then reads a string and searches for it.  */
  43.  
  44. #define BCOPY_MISSING
  45.  
  46.  
  47. #ifdef emacs
  48.  
  49. /* The `emacs' switch turns on certain special matching commands
  50.  that make sense only in emacs. */
  51.  
  52. #include "config.h"
  53. #include "lisp.h"
  54. #include "buffer.h"
  55. #include "syntax.h"
  56.  
  57. #else  /* not emacs */
  58.  
  59. #ifdef BCOPY_MISSING
  60. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  61. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  62. #define bzero(s,n)    memset((s),0,(n))
  63. #else
  64. void bcopy();
  65. short bcmp();
  66. void bzero();
  67. #endif
  68.  
  69. /* Make alloca work the best possible way.  */
  70. #ifdef __GNUC__
  71. #define alloca __builtin_alloca
  72. #else
  73. #ifdef sparc
  74. #include <alloca.h>
  75. #endif
  76. #endif
  77.  
  78. /*
  79.  * Define the syntax stuff, so we can do the \<...\> things.
  80.  */
  81.  
  82. #ifndef Sword /* must be non-zero in some of the tests below... */
  83. #define Sword 1
  84. #endif
  85.  
  86. #define SYNTAX(c) re_syntax_table[c]
  87.  
  88. #ifdef SYNTAX_TABLE
  89.  
  90. char *re_syntax_table;
  91.  
  92. #else
  93.  
  94. static char re_syntax_table[256];
  95.  
  96. static void
  97. init_syntax_once ()
  98. {
  99.    register short c;
  100.    static short done = 0;
  101.  
  102.    if (done)
  103.      return;
  104.  
  105.    bzero (re_syntax_table, sizeof re_syntax_table);
  106.  
  107.    for (c = 'a'; c <= 'z'; c++)
  108.      re_syntax_table[c] = Sword;
  109.  
  110.    for (c = 'A'; c <= 'Z'; c++)
  111.      re_syntax_table[c] = Sword;
  112.  
  113.    for (c = '0'; c <= '9'; c++)
  114.      re_syntax_table[c] = Sword;
  115.  
  116.    done = 1;
  117. }
  118.  
  119. #endif /* SYNTAX_TABLE */
  120. #endif /* not emacs */
  121.  
  122. /* included by AWK.H #include "REGEX.H" */
  123.  
  124. /* Number of failure points to allocate space for initially,
  125.  when matching.  If this number is exceeded, more space is allocated,
  126.  so it is not a hard limit.  */
  127.  
  128. #ifndef NFAILURES
  129. #define NFAILURES 80
  130. #endif /* NFAILURES */
  131.  
  132. /* width of a byte in bits */
  133.  
  134. #define BYTEWIDTH 8
  135.  
  136. #ifndef SIGN_EXTEND_CHAR
  137. #define SIGN_EXTEND_CHAR(x) (x)
  138. #endif
  139.  
  140. static short obscure_syntax = 0;
  141.  
  142. /* Specify the precise syntax of regexp for compilation.
  143.    This provides for compatibility for various utilities
  144.    which historically have different, incompatible syntaxes.
  145.  
  146.    The argument SYNTAX is a bit-mask containing the two bits
  147.    RE_NO_BK_PARENS and RE_NO_BK_VBAR.  */
  148.  
  149. short
  150. re_set_syntax (short syntax)
  151. {
  152.   short ret;
  153.  
  154.   ret = obscure_syntax;
  155.   obscure_syntax = syntax;
  156.   return ret;
  157. }
  158.  
  159. /* re_compile_pattern takes a regular-expression string
  160.    and converts it into a buffer full of byte commands for matching.
  161.  
  162.   PATTERN   is the address of the pattern string
  163.   SIZE      is the length of it.
  164.   BUFP        is a  struct re_pattern_buffer *  which points to the info
  165.         on where to store the byte commands.
  166.         This structure contains a  char *  which points to the
  167.         actual space, which should have been obtained with malloc.
  168.         re_compile_pattern may use  realloc  to grow the buffer space.
  169.  
  170.   The number of bytes of commands can be found out by looking in
  171.   the  struct re_pattern_buffer  that bufp pointed to,
  172.   after re_compile_pattern returns.
  173. */
  174.  
  175. #define PATPUSH(ch) (*b++ = (char) (ch))
  176.  
  177. #define PATFETCH(c) \
  178.  {if (p == pend) goto end_of_pattern; \
  179.   c = * (unsigned char *) p++; \
  180.   if (translate) c = translate[c]; }
  181.  
  182. #define PATFETCH_RAW(c) \
  183.  {if (p == pend) goto end_of_pattern; \
  184.   c = * (unsigned char *) p++; }
  185.  
  186. #define PATUNFETCH p--
  187.  
  188. #ifdef MSDOS
  189. #define MaxAllocation (1<<14)
  190. #else
  191. /* TEST was 1<<16 */
  192. #define MaxAllocation (1<<14) /* 16,384 */
  193. #endif
  194.  
  195. /* Mac note: the ibm/unix version of EXTEND_BUFFER had
  196.     c = (unsigned)(bufp->buffer - old_buffer); with c being unsigned short.
  197. This assumes that 1) reallocation will always be upwards in memory, and
  198. 2) the amount of shift will fit in an unsigned short. Both of these assumptions
  199. are false on a Mac. The Mac version uses
  200.     bufferShift = bufp->buffer - old_buffer; with bufferShift being a long short
  201. declared in re_compile_pattern, which is the only function where EXTEND_BUFFER
  202. is used.
  203. */
  204. #define EXTEND_BUFFER \
  205.   { char *old_buffer = bufp->buffer; \
  206.     if (bufp->allocated == MaxAllocation) goto too_big; \
  207.     bufp->allocated *= 2; \
  208.     if (bufp->allocated > MaxAllocation) bufp->allocated = MaxAllocation; \
  209.     if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  210.       goto memory_exhausted; \
  211.     bufferShift = bufp->buffer - old_buffer; \
  212.     b += bufferShift; \
  213.     if (fixup_jump) \
  214.       fixup_jump += bufferShift; \
  215.     if (laststart) \
  216.       laststart += bufferShift; \
  217.     begalt += bufferShift; \
  218.     if (pending_exact) \
  219.       pending_exact += bufferShift; \
  220.   }
  221.  
  222. #ifdef NEVER /* not defined - NOT CORRECTED FOR MAC - see note for EXTEND_BUFFER above. */
  223. #define EXTEND_BUFFER \
  224.   { unsigned b_off = b - bufp->buffer, \
  225.              f_off, l_off, p_off, \
  226.                 beg_off = begalt - bufp->buffer; \
  227.     if (fixup_jump) \
  228.        f_off = fixup_jump - bufp->buffer; \
  229.     if (laststart) \
  230.        l_off = laststart - bufp->buffer; \
  231.     if (pending_exact) \
  232.        p_off = pending_exact - bufp->buffer; \
  233.     if (bufp->allocated == MaxAllocation) goto too_big; \
  234.     bufp->allocated *= 2; \
  235.     if (bufp->allocated > MaxAllocation) bufp->allocated = MaxAllocation; \
  236.     if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  237.       goto memory_exhausted; \
  238.     b = bufp->buffer + b_off; \
  239.     if (fixup_jump) \
  240.       fixup_jump = bufp->buffer + f_off; \
  241.     if (laststart) \
  242.       laststart = bufp->buffer + l_off; \
  243.     begalt = bufp->buffer + beg_off; \
  244.     if (pending_exact) \
  245.       pending_exact = bufp->buffer + p_off; \
  246.   }
  247. #endif
  248.  
  249. char *
  250. re_compile_pattern(char *pattern,short size,struct re_pattern_buffer *bufp)
  251. {
  252.   register char *b = bufp->buffer;
  253.   register char *p = pattern;
  254.   char *pend = pattern + size;
  255.   register unsigned c, c1;
  256.   char *p1;
  257.   unsigned char *translate = (unsigned char *) bufp->translate;
  258.  
  259.   /* address of the count-byte of the most recently inserted "exactn" command.
  260.     This makes it possible to tell whether a new exact-match character
  261.     can be added to that command or requires a new "exactn" command. */
  262.      
  263.   char *pending_exact = 0;
  264.  
  265.   /* address of the place where a forward-jump should go
  266.     to the end of the containing expression.
  267.     Each alternative of an "or", except the last, ends with a forward-jump
  268.     of this sort. */
  269.  
  270.   char *fixup_jump = 0;
  271.  
  272.   /* address of start of the most recently finished expression.
  273.     This tells postfix * where to find the start of its operand. */
  274.  
  275.   char *laststart = 0;
  276.  
  277.   /* In processing a repeat, 1 means zero matches is allowed */
  278.  
  279.   char zero_times_ok;
  280.  
  281.   /* In processing a repeat, 1 means many matches is allowed */
  282.  
  283.   char many_times_ok;
  284.  
  285.   /* address of beginning of regexp, or inside of last \( */
  286.  
  287.   char *begalt = b;
  288.  
  289.   /* Stack of information saved by \( and restored by \).
  290.      Four stack elements are pushed by each \(:
  291.        First, the value of b.
  292.        Second, the value of fixup_jump.
  293.        Third, the value of regnum.
  294.        Fourth, the value of begalt.  */
  295.  
  296.   short stackb[40];
  297.   short *stackp = stackb;
  298.   short *stacke = stackb + 40;
  299.   short *stackt;
  300.  
  301.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  302.      where it becomes the "register number" to put in the stop_memory command */
  303.  
  304.   short regnum = 1;
  305.   
  306.   long    bufferShift; /* For the Mac version. See EXTEND_BUFFER above. */
  307.  
  308.   bufp->fastmap_accurate = 0;
  309.  
  310. #ifndef emacs
  311. #ifndef SYNTAX_TABLE
  312.   /*
  313.    * Initialize the syntax table.
  314.    */
  315.    init_syntax_once();
  316. #endif
  317. #endif
  318.  
  319.   if (bufp->allocated == 0)
  320.     {
  321.       bufp->allocated = 28;
  322.       if (bufp->buffer)
  323.     /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  324.     bufp->buffer = (char *) realloc (bufp->buffer, 28);
  325.       else
  326.     /* Caller did not allocate a buffer.  Do it for him */
  327.     bufp->buffer = (char *) malloc (28);
  328.       if (!bufp->buffer) goto memory_exhausted;
  329.       begalt = b = bufp->buffer;
  330.     }
  331.  
  332.   while (p != pend)
  333.     {
  334.       if ((short)(b - bufp->buffer) > bufp->allocated - 10)
  335.     /* Note that EXTEND_BUFFER clobbers c. Mac note, not only did it clobber c,
  336.     it crashed the machine. Now THAT's a clobber! */
  337.     EXTEND_BUFFER;
  338.  
  339.       PATFETCH (c);
  340.  
  341.       switch (c)
  342.     {
  343.     case '$':
  344.       if (obscure_syntax & RE_TIGHT_VBAR)
  345.         {
  346.           if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p != pend)
  347.         goto normal_char;
  348.           /* Make operand of last vbar end before this `$'.  */
  349.           if (fixup_jump)
  350.         store_jump (fixup_jump, jump, b);
  351.           fixup_jump = 0;
  352.           PATPUSH (endline);
  353.           break;
  354.         }
  355.  
  356.       /* $ means succeed if at end of line, but only in special contexts.
  357.         If randomly in the middle of a pattern, it is a normal character. */
  358.       if (p == pend || *p == CR
  359.           || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  360.           || (obscure_syntax & RE_NO_BK_PARENS
  361.           ? *p == ')'
  362.           : *p == '\\' && p[1] == ')')
  363.           || (obscure_syntax & RE_NO_BK_VBAR
  364.           ? *p == '|'
  365.           : *p == '\\' && p[1] == '|'))
  366.         {
  367.           PATPUSH (endline);
  368.           break;
  369.         }
  370.       goto normal_char;
  371.  
  372.     case '^':
  373.       /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  374.  
  375.       if (laststart && p[-2] != CR
  376.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  377.         goto normal_char;
  378.       if (obscure_syntax & RE_TIGHT_VBAR)
  379.         {
  380.           if (p != pattern + 1
  381.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  382.         goto normal_char;
  383.           PATPUSH (begline);
  384.           begalt = b;
  385.         }
  386.       else
  387.         PATPUSH (begline);
  388.       break;
  389.  
  390.     case '+':
  391.     case '?':
  392.       if (obscure_syntax & RE_BK_PLUS_QM)
  393.         goto normal_char;
  394.     handle_plus:
  395.     case '*':
  396.       /* If there is no previous pattern, char not special. */
  397.       if (!laststart && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  398.         goto normal_char;
  399.       /* If there is a sequence of repetition chars,
  400.          collapse it down to equivalent to just one.  */
  401.       zero_times_ok = 0;
  402.       many_times_ok = 0;
  403.       while (1)
  404.         {
  405.           zero_times_ok |= c != '+';
  406.           many_times_ok |= c != '?';
  407.           if (p == pend)
  408.         break;
  409.           PATFETCH (c);
  410.           if (c == '*')
  411.         ;
  412.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  413.                && (c == '+' || c == '?'))
  414.         ;
  415.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  416.                && c == '\\')
  417.         {
  418.           short c1;
  419.           PATFETCH (c1);
  420.           if (!(c1 == '+' || c1 == '?'))
  421.             {
  422.               PATUNFETCH;
  423.               PATUNFETCH;
  424.               break;
  425.             }
  426.           c = c1;
  427.         }
  428.           else
  429.         {
  430.           PATUNFETCH;
  431.           break;
  432.         }
  433.         }
  434.  
  435.       /* Star, etc. applied to an empty pattern is equivalent
  436.          to an empty pattern.  */
  437.       if (!laststart)
  438.         break;
  439.  
  440.       /* Now we know whether 0 matches is allowed,
  441.          and whether 2 or more matches is allowed.  */
  442.       if (many_times_ok)
  443.         {
  444.           /* If more than one repetition is allowed,
  445.          put in a backward jump at the end.  */
  446.           store_jump (b, maybe_finalize_jump, laststart - 3);
  447.           b += 3;
  448.         }
  449.       insert_jump (on_failure_jump, laststart, b + 3, b);
  450.       pending_exact = 0;
  451.       b += 3;
  452.       if (!zero_times_ok)
  453.         {
  454.           /* At least one repetition required: insert before the loop
  455.          a skip over the initial on-failure-jump instruction */
  456.           insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  457.           b += 3;
  458.         }
  459.       break;
  460.  
  461.     case '.':
  462.       laststart = b;
  463.       PATPUSH (anychar);
  464.       break;
  465.  
  466.     case '[':
  467.       while ((short)(b - bufp->buffer)
  468.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  469.         /* Note that EXTEND_BUFFER clobbers c. Mac note, no longer. */
  470.         EXTEND_BUFFER;
  471.  
  472.       laststart = b;
  473.       if (*p == '^')
  474.         PATPUSH (charset_not), p++;
  475.       else
  476.         PATPUSH (charset);
  477.       p1 = p;
  478.  
  479.       PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  480.       /* Clear the whole map */
  481.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  482.       /* Read in characters and ranges, setting map bits */
  483.       while (1)
  484.         {
  485.           PATFETCH (c);
  486.  
  487.           /* If awk, \ escapes characters inside [...].  */
  488.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  489.             {
  490.               PATFETCH(c1);
  491.               b[c1 / BYTEWIDTH] |= 1 << (c1 % BYTEWIDTH);
  492.               continue;
  493.             }
  494.  
  495.           if (c == ']' && p != p1 + 1) break;
  496.           if (*p == '-' && p[1] != ']')
  497.         {
  498.           PATFETCH (c1);
  499.           PATFETCH (c1);
  500.           while (c <= c1)
  501.             b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  502.         }
  503.           else
  504.         {
  505.           b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  506.         }
  507.         }
  508.       /* Discard any bitmap bytes that are all 0 at the end of the map.
  509.          Decrement the map-length byte too. */
  510.       while ((short) b[-1] > 0 && b[b[-1] - 1] == 0)
  511.         b[-1]--;
  512.       b += b[-1];
  513.       break;
  514.  
  515.     case '(':
  516.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  517.         goto normal_char;
  518.       else
  519.         goto handle_open;
  520.  
  521.     case ')':
  522.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  523.         goto normal_char;
  524.       else
  525.         goto handle_close;
  526.  
  527.     case CR:
  528.       if (! (obscure_syntax & RE_NEWLINE_OR))
  529.         goto normal_char;
  530.       else
  531.         goto handle_bar;
  532.  
  533.     case '|':
  534.       if (! (obscure_syntax & RE_NO_BK_VBAR))
  535.         goto normal_char;
  536.       else
  537.         goto handle_bar;
  538.  
  539.     case '\\':
  540.       if (p == pend) goto invalid_pattern;
  541.       PATFETCH_RAW (c);
  542.       switch (c)
  543.         {
  544.         case '(':
  545.           if (obscure_syntax & RE_NO_BK_PARENS)
  546.         goto normal_backsl;
  547.         handle_open:
  548.           if (stackp == stacke) goto nesting_too_deep;
  549.           if (regnum < RE_NREGS)
  550.             {
  551.           PATPUSH (start_memory);
  552.           PATPUSH (regnum);
  553.             }
  554.           *stackp++ = (short)(b - bufp->buffer);
  555.           *stackp++ = fixup_jump ? (short)(fixup_jump - bufp->buffer) + 1 : 0;
  556.           *stackp++ = regnum++;
  557.           *stackp++ = (short)(begalt - bufp->buffer);
  558.           fixup_jump = 0;
  559.           laststart = 0;
  560.           begalt = b;
  561.           break;
  562.  
  563.         case ')':
  564.           if (obscure_syntax & RE_NO_BK_PARENS)
  565.         goto normal_backsl;
  566.         handle_close:
  567.           if (stackp == stackb) goto unmatched_close;
  568.           begalt = *--stackp + bufp->buffer;
  569.           if (fixup_jump)
  570.         store_jump (fixup_jump, jump, b);
  571.           if (stackp[-1] < RE_NREGS)
  572.         {
  573.           PATPUSH (stop_memory);
  574.           PATPUSH (stackp[-1]);
  575.         }
  576.           stackp -= 2;
  577.           fixup_jump = 0;
  578.           if (*stackp)
  579.         fixup_jump = *stackp + bufp->buffer - 1;
  580.           laststart = *--stackp + bufp->buffer;
  581.           break;
  582.  
  583.         case '|':
  584.           if (obscure_syntax & RE_NO_BK_VBAR)
  585.         goto normal_backsl;
  586.         handle_bar:
  587.           insert_jump (on_failure_jump, begalt, b + 6, b);
  588.           pending_exact = 0;
  589.           b += 3;
  590.           if (fixup_jump)
  591.         store_jump (fixup_jump, jump, b);
  592.           fixup_jump = b;
  593.           b += 3;
  594.           laststart = 0;
  595.           begalt = b;
  596.           break;
  597.  
  598. #ifdef emacs
  599.         case '=':
  600.           PATPUSH (at_dot);
  601.           break;
  602.  
  603.         case 's':    
  604.           laststart = b;
  605.           PATPUSH (syntaxspec);
  606.           PATFETCH (c);
  607.           PATPUSH (syntax_spec_code[c]);
  608.           break;
  609.  
  610.         case 'S':
  611.           laststart = b;
  612.           PATPUSH (notsyntaxspec);
  613.           PATFETCH (c);
  614.           PATPUSH (syntax_spec_code[c]);
  615.           break;
  616. #endif /* emacs */
  617.  
  618.         case 'w':
  619.           laststart = b;
  620.           PATPUSH (wordchar);
  621.           break;
  622.  
  623.         case 'W':
  624.           laststart = b;
  625.           PATPUSH (notwordchar);
  626.           break;
  627.  
  628.         case '<':
  629.           PATPUSH (wordbeg);
  630.           break;
  631.  
  632.         case '>':
  633.           PATPUSH (wordend);
  634.           break;
  635.  
  636.         case 'b':
  637.           PATPUSH (wordbound);
  638.           break;
  639.  
  640.         case 'B':
  641.           PATPUSH (notwordbound);
  642.           break;
  643.  
  644.         case '`':
  645.           PATPUSH (begbuf);
  646.           break;
  647.  
  648.         case '\'':
  649.           PATPUSH (endbuf);
  650.           break;
  651.  
  652.         case '1':
  653.         case '2':
  654.         case '3':
  655.         case '4':
  656.         case '5':
  657.         case '6':
  658.         case '7':
  659.         case '8':
  660.         case '9':
  661.           c1 = c - '0';
  662.           if (c1 >= regnum)
  663.         goto normal_char;
  664.           for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  665.          if (*stackt == c1)
  666.           goto normal_char;
  667.           laststart = b;
  668.           PATPUSH (duplicate);
  669.           PATPUSH (c1);
  670.           break;
  671.  
  672.         case '+':
  673.         case '?':
  674.           if (obscure_syntax & RE_BK_PLUS_QM)
  675.         goto handle_plus;
  676.  
  677.         default:
  678.         normal_backsl:
  679.           /* You might think it would be useful for \ to mean
  680.          not to translate; but if we don't translate it
  681.          it will never match anything.  */
  682.           if (translate) c = translate[c];
  683.           goto normal_char;
  684.         }
  685.       break;
  686.  
  687.     default:
  688.     normal_char:
  689.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  690.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  691.           || ((obscure_syntax & RE_BK_PLUS_QM)
  692.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  693.           : (*p == '+' || *p == '?')))
  694.         {
  695.           laststart = b;
  696.           PATPUSH (exactn);
  697.           pending_exact = b;
  698.           PATPUSH (0);
  699.         }
  700.       PATPUSH (c);
  701.       (*pending_exact)++;
  702.     }
  703.     }
  704.  
  705.   if (fixup_jump)
  706.     store_jump (fixup_jump, jump, b);
  707.  
  708.   if (stackp != stackb) goto unmatched_open;
  709.  
  710.   bufp->used = b - bufp->buffer;
  711.   return 0;
  712.  
  713.  invalid_pattern:
  714.   return "Invalid regular expression";
  715.  
  716.  unmatched_open:
  717.   return "Unmatched \\(";
  718.  
  719.  unmatched_close:
  720.   return "Unmatched \\)";
  721.  
  722.  end_of_pattern:
  723.   return "Premature end of regular expression";
  724.  
  725.  nesting_too_deep:
  726.   return "Nesting too deep";
  727.  
  728.  too_big:
  729.   return "Regular expression too big";
  730.  
  731.  memory_exhausted:
  732.   return "Memory exhausted";
  733. }
  734.  
  735. /* Store where `from' points a jump operation to jump to where `to' points.
  736.   `opcode' is the opcode to store. */
  737.  
  738. static short
  739. store_jump(char *from,char opcode,char *to)
  740. {
  741.   from[0] = opcode;
  742.   from[1] = (to - (from + 3)) & 0377;
  743.   from[2] = (to - (from + 3)) >> 8;
  744. }
  745.  
  746. /* Open up space at char FROM, and insert there a jump to TO.
  747.    CURRENT_END gives te end of the storage no in use,
  748.    so we know how much data to copy up.
  749.    OP is the opcode of the jump to insert.
  750.  
  751.    If you call this function, you must zero out pending_exact.  */
  752.  
  753. static short
  754. insert_jump(char op,char *from,char *to,char *current_end)
  755. {
  756.   register char *pto = current_end + 3;
  757.   register char *pfrom = current_end;
  758.   while (pfrom != from)
  759.     *--pto = *--pfrom;
  760.   store_jump (from, op, to);
  761. }
  762.  
  763. /* Given a pattern, compute a fastmap from it.
  764.  The fastmap records which of the (1 << BYTEWIDTH) possible characters
  765.  can start a string that matches the pattern.
  766.  This fastmap is used by re_search to skip quickly over totally implausible text.
  767.  
  768.  The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  769.  as bufp->fastmap.
  770.  The other components of bufp describe the pattern to be used.  */
  771.  
  772. void
  773. re_compile_fastmap (bufp)
  774.      struct re_pattern_buffer *bufp;
  775. {
  776.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  777.   short size = bufp->used;
  778.   register char *fastmap = bufp->fastmap;
  779.   register unsigned char *p = pattern;
  780.   register unsigned char *pend = pattern + size;
  781.   register short j, k;
  782.   unsigned char *translate = (unsigned char *) bufp->translate;
  783.  
  784.   unsigned char *stackb[NFAILURES];
  785.   unsigned char **stackp = stackb;
  786.  
  787.   bzero (fastmap, (1 << BYTEWIDTH));
  788.   bufp->fastmap_accurate = 1;
  789.   bufp->can_be_null = 0;
  790.       
  791.   while (p)
  792.     {
  793.       if (p == pend)
  794.     {
  795.       bufp->can_be_null = 1;
  796.       break;
  797.     }
  798. #ifdef SWITCH_ENUM_BUG
  799.       switch ((short) ((enum regexpcode) *p++))
  800. #else
  801.       switch ((enum regexpcode) *p++)
  802. #endif
  803.     {
  804.     case exactn:
  805.       if (translate)
  806.         fastmap[translate[p[1]]] = 1;
  807.       else
  808.         fastmap[p[1]] = 1;
  809.       break;
  810.  
  811.         case begline:
  812.         case before_dot:
  813.     case at_dot:
  814.     case after_dot:
  815.     case begbuf:
  816.     case endbuf:
  817.     case wordbound:
  818.     case notwordbound:
  819.     case wordbeg:
  820.     case wordend:
  821.       continue;
  822.  
  823.     case endline:
  824.       if (translate)
  825.         fastmap[translate[CR]] = 1;
  826.       else
  827.         fastmap[CR] = 1;
  828.       if (bufp->can_be_null != 1)
  829.         bufp->can_be_null = 2;
  830.       break;
  831.  
  832.     case finalize_jump:
  833.     case maybe_finalize_jump:
  834.     case jump:
  835.     case dummy_failure_jump:
  836.       bufp->can_be_null = 1;
  837.       j = *p++ & 0377;
  838.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  839.       p += j + 1;        /* The 1 compensates for missing ++ above */
  840.       if (j > 0)
  841.         continue;
  842.       /* Jump backward reached implies we just went through
  843.          the body of a loop and matched nothing.
  844.          Opcode jumped to should be an on_failure_jump.
  845.          Just treat it like an ordinary jump.
  846.          For a * loop, it has pushed its failure point already;
  847.          if so, discard that as redundant.  */
  848.       if ((enum regexpcode) *p != on_failure_jump)
  849.         continue;
  850.       p++;
  851.       j = *p++ & 0377;
  852.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  853.       p += j + 1;        /* The 1 compensates for missing ++ above */
  854.       if (stackp != stackb && *stackp == p)
  855.         stackp--;
  856.       continue;
  857.       
  858.     case on_failure_jump:
  859.       j = *p++ & 0377;
  860.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  861.       p++;
  862.       *++stackp = p + j;
  863.       continue;
  864.  
  865.     case start_memory:
  866.     case stop_memory:
  867.       p++;
  868.       continue;
  869.  
  870.     case duplicate:
  871.       bufp->can_be_null = 1;
  872.       fastmap[CR] = 1;
  873.     case anychar:
  874.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  875.         if (j != CR)
  876.           fastmap[j] = 1;
  877.       if (bufp->can_be_null)
  878.         return;
  879.       /* Don't return; check the alternative paths
  880.          so we can set can_be_null if appropriate.  */
  881.       break;
  882.  
  883.     case wordchar:
  884.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  885.         if (SYNTAX (j) == Sword)
  886.           fastmap[j] = 1;
  887.       break;
  888.  
  889.     case notwordchar:
  890.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  891.         if (SYNTAX (j) != Sword)
  892.           fastmap[j] = 1;
  893.       break;
  894.  
  895. #ifdef emacs
  896.     case syntaxspec:
  897.       k = *p++;
  898.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  899.         if (SYNTAX (j) == (enum syntaxcode) k)
  900.           fastmap[j] = 1;
  901.       break;
  902.  
  903.     case notsyntaxspec:
  904.       k = *p++;
  905.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  906.         if (SYNTAX (j) != (enum syntaxcode) k)
  907.           fastmap[j] = 1;
  908.       break;
  909. #endif /* emacs */
  910.  
  911.     case charset:
  912.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  913.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  914.           {
  915.         if (translate)
  916.           fastmap[translate[j]] = 1;
  917.         else
  918.           fastmap[j] = 1;
  919.           }
  920.       break;
  921.  
  922.     case charset_not:
  923.       /* Chars beyond end of map must be allowed */
  924.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  925.         if (translate)
  926.           fastmap[translate[j]] = 1;
  927.         else
  928.           fastmap[j] = 1;
  929.  
  930.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  931.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  932.           {
  933.         if (translate)
  934.           fastmap[translate[j]] = 1;
  935.         else
  936.           fastmap[j] = 1;
  937.           }
  938.       break;
  939.     }
  940.  
  941.       /* Get here means we have successfully found the possible starting characters
  942.      of one path of the pattern.  We need not follow this path any farther.
  943.      Instead, look at the next alternative remembered in the stack. */
  944.       if (stackp != stackb)
  945.     p = *stackp--;
  946.       else
  947.     break;
  948.     }
  949. }
  950.  
  951. /* Like re_search_2, below, but only one string is specified. */
  952.  
  953. short
  954. re_search(struct re_pattern_buffer *pbufp,char *string,short size,short startpos,short range,struct re_registers *regs)
  955. {
  956.   return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  957. }
  958.  
  959. /* Like re_match_2 but tries first a match starting at index STARTPOS,
  960.    then at STARTPOS + 1, and so on.
  961.    RANGE is the number of places to try before giving up.
  962.    If RANGE is negative, the starting positions tried are
  963.     STARTPOS, STARTPOS - 1, etc.
  964.    It is up to the caller to make sure that range is not so large
  965.    as to take the starting position outside of the input strings.
  966.  
  967. The value returned is the position at which the match was found,
  968.  or -1 if no match was found,
  969.  or -2 if error (such as failure stack overflow).  */
  970.  
  971. short
  972. re_search_2(struct re_pattern_buffer *pbufp,char *string1,short size1,char *string2,short size2,short startpos,short range,struct re_registers *regs,short mstop)
  973. {
  974.   register char *fastmap = pbufp->fastmap;
  975.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  976.   short total = size1 + size2;
  977.   short val;
  978.  
  979.   /* Update the fastmap now if not correct already */
  980.   if (fastmap && !pbufp->fastmap_accurate)
  981.     re_compile_fastmap (pbufp);
  982.   
  983.   /* Don't waste time in a long search for a pattern
  984.      that says it is anchored.  */
  985.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  986.       && range > 0)
  987.     {
  988.       if (startpos > 0)
  989.     return -1;
  990.       else
  991.     range = 1;
  992.     }
  993.  
  994.   while (1)
  995.     {
  996.       /* If a fastmap is supplied, skip quickly over characters
  997.      that cannot possibly be the start of a match.
  998.      Note, however, that if the pattern can possibly match
  999.      the null string, we must test it at each starting point
  1000.      so that we take the first null string we get.  */
  1001.  
  1002.       if (fastmap && startpos < total && pbufp->can_be_null != 1)
  1003.     {
  1004.       if (range > 0)
  1005.         {
  1006.           register short lim = 0;
  1007.           register unsigned char *p;
  1008.           short irange = range;
  1009.           if (startpos < size1 && startpos + range >= size1)
  1010.         lim = range - (size1 - startpos);
  1011.  
  1012.           p = ((unsigned char *)
  1013.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1014.  
  1015.           if (translate)
  1016.         {
  1017.           while (range > lim && !fastmap[translate[*p++]])
  1018.             range--;
  1019.         }
  1020.           else
  1021.         {
  1022.           while (range > lim && !fastmap[*p++])
  1023.             range--;
  1024.         }
  1025.           startpos += irange - range;
  1026.         }
  1027.       else
  1028.         {
  1029.           register unsigned char c;
  1030.           if (startpos >= size1)
  1031.         c = string2[startpos - size1];
  1032.           else
  1033.         c = string1[startpos];
  1034.           c &= 0xff;
  1035.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1036.         goto advance;
  1037.         }
  1038.     }
  1039.  
  1040.       if (range >= 0 && startpos == total
  1041.       && fastmap && pbufp->can_be_null == 0)
  1042.     return -1;
  1043.  
  1044.       val = re_match_2 (pbufp, (unsigned char *)string1, size1, 
  1045.           (unsigned char *)string2, size2, startpos, regs, mstop);
  1046.       if (0 <= val)
  1047.     {
  1048.       if (val == -2)
  1049.         return -2;
  1050.       return startpos;
  1051.     }
  1052.  
  1053. #ifdef C_ALLOCA
  1054.       alloca (0);
  1055. #endif /* C_ALLOCA */
  1056.  
  1057.     advance:
  1058.       if (!range) break;
  1059.       if (range > 0) range--, startpos++; else range++, startpos--;
  1060.     }
  1061.   return -1;
  1062. }
  1063.  
  1064. #ifndef emacs   /* emacs never uses this */
  1065. short
  1066. re_match(struct re_pattern_buffer *pbufp,char *string,short size,short pos,struct re_registers *regs)
  1067. {
  1068.   return re_match_2 (pbufp, (unsigned char *)0, 0, (unsigned char *)string, 
  1069.       size, pos, regs, size);
  1070. }
  1071. #endif /* emacs */
  1072.  
  1073. /* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1074.  
  1075. short re_max_failures = 2000;
  1076.  
  1077.  
  1078. /* Match the pattern described by PBUFP
  1079.    against data which is the virtual concatenation of STRING1 and STRING2.
  1080.    SIZE1 and SIZE2 are the sizes of the two data strings.
  1081.    Start the match at position POS.
  1082.    Do not consider matching past the position MSTOP.
  1083.  
  1084.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1085.  
  1086.    The reason that the data to match are specified as two components
  1087.    which are to be regarded as concatenated
  1088.    is so this function can be used directly on the contents of an Emacs buffer.
  1089.  
  1090.    -1 is returned if there is no match.  -2 is returned if there is
  1091.    an error (such as match stack overflow).  Otherwise the value is the length
  1092.    of the substring which was matched.  */
  1093.  
  1094. short
  1095. re_match_2(struct re_pattern_buffer *pbufp,unsigned char *string1,short size1,unsigned char *string2,short size2,short pos,struct re_registers *regs,short mstop)
  1096. {
  1097.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1098.   register unsigned char *pend = p + pbufp->used;
  1099.   /* End of first string */
  1100.   unsigned char *end1;
  1101.   /* End of second string */
  1102.   unsigned char *end2;
  1103.   /* Pointer just past last char to consider matching */
  1104.   unsigned char *end_match_1, *end_match_2;
  1105.   register unsigned char *d, *dend;
  1106.   register short mcnt;
  1107.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1108.  
  1109.  /* Failure point stack.  Each place that can handle a failure further down the line
  1110.     pushes a failure point on this stack.  It consists of two char *'s.
  1111.     The first one pushed is where to resume scanning the pattern;
  1112.     the second pushed is where to resume scanning the strings.
  1113.     If the latter is zero, the failure point is a "dummy".
  1114.     If a failure happens and the innermost failure point is dormant,
  1115.     it discards that failure point and tries the next one. */
  1116.  
  1117.   unsigned char *initial_stack[2 * NFAILURES];
  1118.   unsigned char **stackb = initial_stack;
  1119.   unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1120.  
  1121.   /* Information on the "contents" of registers.
  1122.      These are pointers into the input strings; they record
  1123.      just what was matched (on this attempt) by some part of the pattern.
  1124.      The start_memory command stores the start of a register's contents
  1125.      and the stop_memory command stores the end.
  1126.  
  1127.      At that point, regstart[regnum] points to the first character in the register,
  1128.      regend[regnum] points to the first character beyond the end of the register,
  1129.      regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1130.      and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1131.  
  1132.   unsigned char *regstart[RE_NREGS];
  1133.   unsigned char *regend[RE_NREGS];
  1134.   unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1135.  
  1136.   /* Set up pointers to ends of strings.
  1137.      Don't allow the second string to be empty unless both are empty.  */
  1138.   if (!size2)
  1139.     {
  1140.       string2 = string1;
  1141.       size2 = size1;
  1142.       string1 = 0;
  1143.       size1 = 0;
  1144.     }
  1145.   end1 = string1 + size1;
  1146.   end2 = string2 + size2;
  1147.  
  1148.   /* Compute where to stop matching, within the two strings */
  1149.   if (mstop <= size1)
  1150.     {
  1151.       end_match_1 = string1 + mstop;
  1152.       end_match_2 = string2;
  1153.     }
  1154.   else
  1155.     {
  1156.       end_match_1 = end1;
  1157.       end_match_2 = string2 + mstop - size1;
  1158.     }
  1159.  
  1160.   /* Initialize \) text positions to -1
  1161.      to mark ones that no \( or \) has been seen for.  */
  1162.  
  1163.   for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1164.     regend[mcnt] = (unsigned char *) -1;
  1165.  
  1166.   /* `p' scans through the pattern as `d' scans through the data.
  1167.      `dend' is the end of the input string that `d' points within.
  1168.      `d' is advanced into the following input string whenever necessary,
  1169.      but this happens before fetching;
  1170.      therefore, at the beginning of the loop,
  1171.      `d' can be pointing at the end of a string,
  1172.      but it cannot equal string2.  */
  1173.  
  1174.   if (pos <= size1)
  1175.     d = string1 + pos, dend = end_match_1;
  1176.   else
  1177.     d = string2 + pos - size1, dend = end_match_2;
  1178.  
  1179. /* Write PREFETCH; just before fetching a character with *d.  */
  1180. #define PREFETCH \
  1181.  while (d == dend)                            \
  1182.   { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1183.     d = string2;  /* end of string1 => advance to string2. */       \
  1184.     dend = end_match_2; }
  1185.  
  1186.   /* This loop loops over pattern commands.
  1187.      It exits by returning from the function if match is complete,
  1188.      or it drops through if match fails at this starting point in the input data. */
  1189.  
  1190.   while (1)
  1191.     {
  1192.       if (p == pend)
  1193.     /* End of pattern means we have succeeded! */
  1194.     {
  1195.       /* If caller wants register contents data back, convert it to indices */
  1196.       if (regs)
  1197.         {
  1198.            regs->start[0] = pos;
  1199.            if (dend == end_match_1)
  1200.          regs->end[0] = (short)(d - string1);
  1201.            else
  1202.          regs->end[0] = (short)(d - string2) + size1;
  1203.            for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1204.         {
  1205.           if (regend[mcnt] == (unsigned char *) -1)
  1206.             {
  1207.               regs->start[mcnt] = -1;
  1208.               regs->end[mcnt] = -1;
  1209.               continue;
  1210.             }
  1211.            if (regstart_seg1[mcnt])
  1212.             regs->start[mcnt] = (short)(regstart[mcnt] - string1);
  1213.           else
  1214.             regs->start[mcnt] = (short)(regstart[mcnt] - string2) + size1;
  1215.            if (regend_seg1[mcnt])
  1216.             regs->end[mcnt] = (short)(regend[mcnt] - string1);
  1217.           else
  1218.             regs->end[mcnt] = (short)(regend[mcnt] - string2) + size1;
  1219.         }
  1220.         }
  1221.        if (dend == end_match_1)
  1222.         return (d - string1 - pos);
  1223.       else
  1224.         return d - string2 + size1 - pos;
  1225.     }
  1226.  
  1227.       /* Otherwise match next pattern command */
  1228. #ifdef SWITCH_ENUM_BUG
  1229.       switch ((short) ((enum regexpcode) *p++))
  1230. #else
  1231.       switch ((enum regexpcode) *p++)
  1232. #endif
  1233.     {
  1234.  
  1235.     /* \( is represented by a start_memory, \) by a stop_memory.
  1236.         Both of those commands contain a "register number" argument.
  1237.         The text matched within the \( and \) is recorded under that number.
  1238.         Then, \<digit> turns into a `duplicate' command which
  1239.         is followed by the numeric value of <digit> as the register number. */
  1240.  
  1241.     case start_memory:
  1242.       regstart[*p] = d;
  1243.        regstart_seg1[*p++] = (dend == end_match_1);
  1244.       break;
  1245.  
  1246.     case stop_memory:
  1247.       regend[*p] = d;
  1248.        regend_seg1[*p++] = (dend == end_match_1);
  1249.       break;
  1250.  
  1251.     case duplicate:
  1252.       {
  1253.         short regno = *p++;   /* Get which register to match against */
  1254.         register unsigned char *d2, *dend2;
  1255.  
  1256.         d2 = regstart[regno];
  1257.          dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1258.              ? regend[regno] : end_match_1);
  1259.         while (1)
  1260.           {
  1261.         /* Advance to next segment in register contents, if necessary */
  1262.         while (d2 == dend2)
  1263.           {
  1264.             if (dend2 == end_match_2) break;
  1265.             if (dend2 == regend[regno]) break;
  1266.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1267.           }
  1268.         /* At end of register contents => success */
  1269.         if (d2 == dend2) break;
  1270.  
  1271.         /* Advance to next segment in data being matched, if necessary */
  1272.         PREFETCH;
  1273.  
  1274.         /* mcnt gets # consecutive chars to compare */
  1275.         mcnt = (short)(dend - d);
  1276.         if (mcnt > (short)(dend2 - d2))
  1277.           mcnt = (short)(dend2 - d2);
  1278.         /* Compare that many; failure if mismatch, else skip them. */
  1279.         if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1280.           goto fail;
  1281.         d += mcnt, d2 += mcnt;
  1282.           }
  1283.       }
  1284.       break;
  1285.  
  1286.     case anychar:
  1287.       /* fetch a data character */
  1288.       PREFETCH;
  1289.       /* Match anything but a newline.  */
  1290.       if ((translate ? translate[*d++] : *d++) == CR)
  1291.         goto fail;
  1292.       break;
  1293.  
  1294.     case charset:
  1295.     case charset_not:
  1296.       {
  1297.         /* Nonzero for charset_not */
  1298.         short not = 0;
  1299.         register short c;
  1300.         if (*(p - 1) == (unsigned char) charset_not)
  1301.           not = 1;
  1302.  
  1303.         /* fetch a data character */
  1304.         PREFETCH;
  1305.  
  1306.         if (translate)
  1307.           c = translate [*d];
  1308.         else
  1309.           c = *d;
  1310.  
  1311.         if (c < *p * BYTEWIDTH
  1312.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1313.           not = !not;
  1314.  
  1315.         p += 1 + *p;
  1316.  
  1317.         if (!not) goto fail;
  1318.         d++;
  1319.         break;
  1320.       }
  1321.  
  1322.     case begline:
  1323.       if (d == string1 || d[-1] == CR)
  1324.         break;
  1325.       goto fail;
  1326.  
  1327.     case endline:
  1328.       if (d == end2
  1329.           || (d == end1 ? (size2 == 0 || *string2 == CR) : *d == CR))
  1330.         break;
  1331.       goto fail;
  1332.  
  1333.     /* "or" constructs ("|") are handled by starting each alternative
  1334.         with an on_failure_jump that points to the start of the next alternative.
  1335.         Each alternative except the last ends with a jump to the joining point.
  1336.         (Actually, each jump except for the last one really jumps
  1337.          to the following jump, because tensioning the jumps is a hassle.) */
  1338.  
  1339.     /* The start of a stupid repeat has an on_failure_jump that points
  1340.        past the end of the repeat text.
  1341.        This makes a failure point so that, on failure to match a repetition,
  1342.        matching restarts past as many repetitions have been found
  1343.        with no way to fail and look for another one.  */
  1344.  
  1345.     /* A smart repeat is similar but loops back to the on_failure_jump
  1346.        so that each repetition makes another failure point. */
  1347.  
  1348.     case on_failure_jump:
  1349.       if (stackp == stacke)
  1350.         {
  1351.           unsigned char **stackx;
  1352.           if ((short)(stacke - stackb) > re_max_failures * 2)
  1353.         return -2;
  1354.           stackx = (unsigned char **) alloca (2 * (stacke - stackb)
  1355.                      * sizeof (char *));
  1356.           bcopy (stackb, stackx, (short)(stacke - stackb) * sizeof (char *));
  1357.           stackp = stackx + (stackp - stackb);
  1358.           stacke = stackx + 2 * (stacke - stackb);
  1359.           stackb = stackx;
  1360.         }
  1361.       mcnt = *p++ & 0377;
  1362.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1363.       p++;
  1364.       *stackp++ = mcnt + p;
  1365.       *stackp++ = d;
  1366.       break;
  1367.  
  1368.     /* The end of a smart repeat has an maybe_finalize_jump back.
  1369.        Change it either to a finalize_jump or an ordinary jump. */
  1370.  
  1371.     case maybe_finalize_jump:
  1372.       mcnt = *p++ & 0377;
  1373.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1374.       p++;
  1375.       {
  1376.         register unsigned char *p2 = p;
  1377.         /* Compare what follows with the begining of the repeat.
  1378.            If we can establish that there is nothing that they would
  1379.            both match, we can change to finalize_jump */
  1380.         while (p2 != pend
  1381.            && (*p2 == (unsigned char) stop_memory
  1382.                || *p2 == (unsigned char) start_memory))
  1383.           p2++;
  1384.         if (p2 == pend)
  1385.           p[-3] = (unsigned char) finalize_jump;
  1386.         else if (*p2 == (unsigned char) exactn
  1387.              || *p2 == (unsigned char) endline)
  1388.           {
  1389.         register short c = *p2 == (unsigned char) endline ? CR : p2[2];
  1390.         register unsigned char *p1 = p + mcnt;
  1391.         /* p1[0] ... p1[2] are an on_failure_jump.
  1392.            Examine what follows that */
  1393.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1394.           p[-3] = (unsigned char) finalize_jump;
  1395.         else if (p1[3] == (unsigned char) charset
  1396.              || p1[3] == (unsigned char) charset_not)
  1397.           {
  1398.             short not = p1[3] == (unsigned char) charset_not;
  1399.             if (c < p1[4] * BYTEWIDTH
  1400.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1401.               not = !not;
  1402.             /* not is 1 if c would match */
  1403.             /* That means it is not safe to finalize */
  1404.             if (!not)
  1405.               p[-3] = (unsigned char) finalize_jump;
  1406.           }
  1407.           }
  1408.       }
  1409.       p -= 2;
  1410.       if (p[-1] != (unsigned char) finalize_jump)
  1411.         {
  1412.           p[-1] = (unsigned char) jump;
  1413.           goto nofinalize;
  1414.         }
  1415.  
  1416.     /* The end of a stupid repeat has a finalize-jump
  1417.        back to the start, where another failure point will be made
  1418.        which will point after all the repetitions found so far. */
  1419.  
  1420.     case finalize_jump:
  1421.       stackp -= 2;
  1422.  
  1423.     case jump:
  1424.     nofinalize:
  1425.       mcnt = *p++ & 0377;
  1426.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1427.       p += mcnt + 1;    /* The 1 compensates for missing ++ above */
  1428.       break;
  1429.  
  1430.     case dummy_failure_jump:
  1431.       if (stackp == stacke)
  1432.         {
  1433.           unsigned char **stackx
  1434.         = (unsigned char **) alloca (2 * (stacke - stackb)
  1435.                          * sizeof (char *));
  1436.           bcopy (stackb, stackx, (short)(stacke - stackb) * sizeof (char *));
  1437.           stackp = stackx + (stackp - stackb);
  1438.           stacke = stackx + 2 * (stacke - stackb);
  1439.           stackb = stackx;
  1440.         }
  1441.       *stackp++ = 0;
  1442.       *stackp++ = 0;
  1443.       goto nofinalize;
  1444.  
  1445.     case wordbound:
  1446.       if (d == string1  /* Points to first char */
  1447.           || d == end2  /* Points to end */
  1448.           || (d == end1 && size2 == 0)) /* Points to end */
  1449.         break;
  1450.       if ((SYNTAX (d[-1]) == Sword)
  1451.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1452.         break;
  1453.       goto fail;
  1454.  
  1455.     case notwordbound:
  1456.       if (d == string1  /* Points to first char */
  1457.           || d == end2  /* Points to end */
  1458.           || (d == end1 && size2 == 0)) /* Points to end */
  1459.         goto fail;
  1460.       if ((SYNTAX (d[-1]) == Sword)
  1461.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1462.         goto fail;
  1463.       break;
  1464.  
  1465.     case wordbeg:
  1466.       if (d == end2  /* Points to end */
  1467.           || (d == end1 && size2 == 0) /* Points to end */
  1468.           || SYNTAX (* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1469.         goto fail;
  1470.       if (d == string1  /* Points to first char */
  1471.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1472.         break;
  1473.       goto fail;
  1474.  
  1475.     case wordend:
  1476.       if (d == string1  /* Points to first char */
  1477.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1478.         goto fail;
  1479.       if (d == end2  /* Points to end */
  1480.           || (d == end1 && size2 == 0) /* Points to end */
  1481.           || SYNTAX (d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1482.         break;
  1483.       goto fail;
  1484.  
  1485. #ifdef emacs
  1486.     case before_dot:
  1487.       if (((d - string2 <= (unsigned) size2)
  1488.            ? d - bf_p2 : d - bf_p1)
  1489.           <= point)
  1490.         goto fail;
  1491.       break;
  1492.  
  1493.     case at_dot:
  1494.       if (((d - string2 <= (unsigned) size2)
  1495.            ? d - bf_p2 : d - bf_p1)
  1496.           == point)
  1497.         goto fail;
  1498.       break;
  1499.  
  1500.     case after_dot:
  1501.       if (((d - string2 <= (unsigned) size2)
  1502.            ? d - bf_p2 : d - bf_p1)
  1503.           >= point)
  1504.         goto fail;
  1505.       break;
  1506.  
  1507.     case wordchar:
  1508.       mcnt = (short) Sword;
  1509.       goto matchsyntax;
  1510.  
  1511.     case syntaxspec:
  1512.       mcnt = *p++;
  1513.     matchsyntax:
  1514.       PREFETCH;
  1515.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  1516.       break;
  1517.       
  1518.     case notwordchar:
  1519.       mcnt = (short) Sword;
  1520.       goto matchnotsyntax;
  1521.  
  1522.     case notsyntaxspec:
  1523.       mcnt = *p++;
  1524.     matchnotsyntax:
  1525.       PREFETCH;
  1526.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  1527.       break;
  1528. #else
  1529.     case wordchar:
  1530.       PREFETCH;
  1531.       if (SYNTAX (*d++) == 0) goto fail;
  1532.       break;
  1533.       
  1534.     case notwordchar:
  1535.       PREFETCH;
  1536.       if (SYNTAX (*d++) != 0) goto fail;
  1537.       break;
  1538. #endif /* not emacs */
  1539.  
  1540.     case begbuf:
  1541.       if (d == string1)    /* Note, d cannot equal string2 */
  1542.         break;        /* unless string1 == string2.  */
  1543.       goto fail;
  1544.  
  1545.     case endbuf:
  1546.       if (d == end2 || (d == end1 && size2 == 0))
  1547.         break;
  1548.       goto fail;
  1549.  
  1550.     case exactn:
  1551.       /* Match the next few pattern characters exactly.
  1552.          mcnt is how many characters to match. */
  1553.       mcnt = *p++;
  1554.       if (translate)
  1555.         {
  1556.           do
  1557.         {
  1558.           PREFETCH;
  1559.           if (translate[*d++] != *p++) goto fail;
  1560.         }
  1561.           while (--mcnt);
  1562.         }
  1563.       else
  1564.         {
  1565.           do
  1566.         {
  1567.           PREFETCH;
  1568.           if (*d++ != *p++) goto fail;
  1569.         }
  1570.           while (--mcnt);
  1571.         }
  1572.       break;
  1573.     }
  1574.       continue;    /* Successfully matched one pattern command; keep matching */
  1575.  
  1576.       /* Jump here if any matching operation fails. */
  1577.     fail:
  1578.       if (stackp != stackb)
  1579.     /* A restart point is known.  Restart there and pop it. */
  1580.     {
  1581.       if (!stackp[-2])
  1582.         {   /* If innermost failure point is dormant, flush it and keep looking */
  1583.           stackp -= 2;
  1584.           goto fail;
  1585.         }
  1586.       d = *--stackp;
  1587.       p = *--stackp;
  1588.       if (d >= string1 && d <= end1)
  1589.         dend = end_match_1;
  1590.     }
  1591.       else break;   /* Matching at this starting point really fails! */
  1592.     }
  1593.   return -1;         /* Failure to match */
  1594. }
  1595.  
  1596. static short
  1597. bcmp_translate(unsigned char *s1,unsigned char *s2,short len,unsigned char *translate)
  1598. {
  1599.   register unsigned char *p1 = s1, *p2 = s2;
  1600.   while (len)
  1601.     {
  1602.       if (translate [*p1++] != translate [*p2++]) return 1;
  1603.       len--;
  1604.     }
  1605.   return 0;
  1606. }
  1607.  
  1608. /* Entry points compatible with bsd4.2 regex library */
  1609.  
  1610. #ifndef emacs
  1611.  
  1612. static struct re_pattern_buffer re_comp_buf;
  1613.  
  1614. char *
  1615. re_comp (s)
  1616.      char *s;
  1617. {
  1618.   if (!s)
  1619.     {
  1620.       if (!re_comp_buf.buffer)
  1621.     return "No previous regular expression";
  1622.       return 0;
  1623.     }
  1624.  
  1625.   if (!re_comp_buf.buffer)
  1626.     {
  1627.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  1628.     return "Memory exhausted";
  1629.       re_comp_buf.allocated = 200;
  1630.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  1631.     return "Memory exhausted";
  1632.     }
  1633.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  1634. }
  1635.  
  1636. short
  1637. re_exec (s)
  1638.      char *s;
  1639. {
  1640.   short len = strlen (s);
  1641.   return 0 <= re_search (&re_comp_buf, s, len, 0, len, 0);
  1642. }
  1643.  
  1644. #endif /* emacs */
  1645.  
  1646. #ifdef test
  1647.  
  1648. #include <stdio.h>
  1649.  
  1650. /* Indexed by a character, gives the upper case equivalent of the character */
  1651.  
  1652. static char upcase[0400] = 
  1653.   { 000, 001, 002, 003, 004, 005, 006, 007,
  1654.     010, 011, 012, 013, 014, 015, 016, 017,
  1655.     020, 021, 022, 023, 024, 025, 026, 027,
  1656.     030, 031, 032, 033, 034, 035, 036, 037,
  1657.     040, 041, 042, 043, 044, 045, 046, 047,
  1658.     050, 051, 052, 053, 054, 055, 056, 057,
  1659.     060, 061, 062, 063, 064, 065, 066, 067,
  1660.     070, 071, 072, 073, 074, 075, 076, 077,
  1661.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1662.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1663.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1664.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  1665.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1666.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1667.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1668.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  1669.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  1670.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  1671.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  1672.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  1673.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  1674.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  1675.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  1676.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  1677.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  1678.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  1679.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  1680.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  1681.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  1682.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  1683.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  1684.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  1685.   };
  1686.  
  1687. main (argc, argv)
  1688.      short argc;
  1689.      char **argv;
  1690. {
  1691.   char pat[80];
  1692.   struct re_pattern_buffer buf;
  1693.   short i;
  1694.   char c;
  1695.   char fastmap[(1 << BYTEWIDTH)];
  1696.  
  1697.   /* Allow a command argument to specify the style of syntax.  */
  1698.   if (argc > 1)
  1699.     obscure_syntax = atoi (argv[1]);
  1700.  
  1701.   buf.allocated = 40;
  1702.   buf.buffer = (char *) malloc (buf.allocated);
  1703.   buf.fastmap = fastmap;
  1704.   buf.translate = upcase;
  1705.  
  1706.   while (1)
  1707.     {
  1708.       gets (pat);
  1709.  
  1710.       if (*pat)
  1711.     {
  1712.           re_compile_pattern (pat, strlen(pat), &buf);
  1713.  
  1714.       for (i = 0; i < buf.used; i++)
  1715.         printchar (buf.buffer[i]);
  1716.  
  1717.       putchar ('\n');
  1718.  
  1719.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  1720.  
  1721.       re_compile_fastmap (&buf);
  1722.       printf ("Allowed by fastmap: ");
  1723.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  1724.         if (fastmap[i]) printchar (i);
  1725.       putchar ('\n');
  1726.     }
  1727.  
  1728.       gets (pat);    /* Now read the string to match against */
  1729.  
  1730.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  1731.       printf ("Match value %d.\n", i);
  1732.     }
  1733. }
  1734.  
  1735. #ifdef NOTDEF
  1736. print_buf (bufp)
  1737.      struct re_pattern_buffer *bufp;
  1738. {
  1739.   short i;
  1740.  
  1741.   printf ("buf is :\n----------------\n");
  1742.   for (i = 0; i < bufp->used; i++)
  1743.     printchar (bufp->buffer[i]);
  1744.   
  1745.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  1746.   
  1747.   printf ("Allowed by fastmap: ");
  1748.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  1749.     if (bufp->fastmap[i])
  1750.       printchar (i);
  1751.   printf ("\nAllowed by translate: ");
  1752.   if (bufp->translate)
  1753.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  1754.       if (bufp->translate[i])
  1755.     printchar (i);
  1756.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  1757.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  1758. }
  1759. #endif
  1760.  
  1761. printchar (c)
  1762.      char c;
  1763. {
  1764.   if (c < 041 || c >= 0177)
  1765.     {
  1766.       putchar ('\\');
  1767.       putchar (((c >> 6) & 3) + '0');
  1768.       putchar (((c >> 3) & 7) + '0');
  1769.       putchar ((c & 7) + '0');
  1770.     }
  1771.   else
  1772.     putchar (c);
  1773. }
  1774.  
  1775. #endif /* test */
  1776.  
  1777. void InitRegex(void);
  1778.  
  1779. void InitRegex()
  1780.     {
  1781.     obscure_syntax = 0;
  1782.     /* static struct re_pattern_buffer re_comp_buf; - not used */
  1783.     }
  1784.  
  1785. void SaveRegex(void);
  1786. void RestoreRegex(void);
  1787. void SaveRegex()
  1788.     {
  1789.     hs->obscure_syntax = obscure_syntax;
  1790.     }
  1791.  
  1792. void RestoreRegex()
  1793.     {
  1794.     obscure_syntax = hs->obscure_syntax;
  1795.     }
  1796.  
  1797.